package com.hegongda.okcmall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hegongda.common.valid.AddCheck;
import com.hegongda.common.valid.UpdateCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hegongda.okcmall.product.entity.BrandEntity;
import com.hegongda.okcmall.product.service.BrandService;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author Liuweidong
 * @email Liuweidong@gmail.com
 * @date 2021-01-31 17:57:14
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   //  @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   //  @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   //  @RequiresPermissions("product:brand:save")
    public R save(@Validated(AddCheck.class) @RequestBody BrandEntity brand){
       /* if (bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            HashMap<String, String> map = new HashMap<>();
            fieldErrors.stream().forEach(item -> {
                String field = item.getField();
                String defaultMessage = item.getDefaultMessage();
                map.put(field, defaultMessage);
            });
            return R.error(400,"数据校验出错").put("data",map);
        } else {*/
            brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //   @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateCheck.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
 //   @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody BrandEntity brandEntity){
        brandService.updateStatus(brandEntity);
        return R.ok();
    }

}
